<?php

namespace App\Controller\Admin\Common;

use App\Controller\Admin\BaseController;
use App\Service\Common\CategoryService;
use App\Validator\Admin\Common\CategoryValidator;
use Hyperf\Di\Annotation\Inject;

class CategoryController extends BaseController
{

    #[Inject]
    protected CategoryValidator $categoryValidator;

    #[Inject]
    protected CategoryService $categoryService;

    public function pageList()
    {

    }

    public function all()
    {
        $tree = $this->categoryService->getCategoryTreeFromDb();
        return $this->success([
            'tree' => $tree,
        ]);
    }

    public function detail()
    {
    }

    public function add()
    {
        $params = $this->allParams();
        $va = $this->categoryValidator->add($params);
        [$r, $msg] = $this->categoryService->addCategory($va, $this->getAdminId());
        if ($r){
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function update()
    {
        $params = $this->allParams();
        $va = $this->categoryValidator->update($params);
        [$r, $msg] = $this->categoryService->updateCategory(intval($va['id']), $va);
        if ($r){
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function del()
    {
        $params = $this->allParams();
        $va = $this->categoryValidator->del($params);
        [$r, $msg] = $this->categoryService->delCategory($va['ids']);
        if ($r){
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function refreshAll()
    {
        [$r, $msg] = $this->categoryService->refreshTree();
        if ($r){
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }
}